﻿package birdry.core
{
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author 
	 */
	public class  Calendar	{
		
		var _setDate:Date;
		var _nowDate:Date;
		public var dataArr:Array;
		public var totalDate:uint;
		private var _year:uint;
		private var _month:uint;
		private var _distance:Point;
		
		var i:uint;
		var u:uint;
		
		public function Calendar(): void	{
			
		}
		
		public function init( year:uint, month:uint, nowDate:Date, distance:Point): void	{
			
			_year = year;
			_month = month;
			_distance = distance;
			
			month--;
			_setDate = new Date(year, month, 1);
			_nowDate = nowDate;
			dataArr = new Array();
			
			var curLine:uint = 0;
			
			for( i=1; i<37; ++i )	{
				_setDate.setDate(i);
				
				var dateInfo:DateInfo;
				
				// 32까지의 루프에서 해당 월이 넘어가면 남은 공백기간에 해당하는 오브젝트 배열을 생성후 루핑을 멈춘다.
				if (_setDate.getMonth() != month)	{
					if (_setDate.day != 0)	{
						for (u = _setDate.day; u < 7; ++u)	{
							dateInfo = new DateInfo();
							dateInfo.year = _setDate.getFullYear();
							dateInfo.month = _setDate.getMonth();
							dateInfo.date = 0;
							dateInfo.day = u;
							dateInfo.toDay = false;
							dateInfo.xx = u;
							dateInfo.yy = curLine;
							dateInfo.x = dateInfo.xx * _distance.x;
							dateInfo.y = dateInfo.yy * _distance.y;
							dataArr.push(dateInfo);
						}
					}
					break;
				}
				
				//	1일 이전의 공백날짜에 해당하는 배열에 오브젝트를 생성해준다.
				if( i ==1 )	{
					for ( u = 0;  u < _setDate.getDay(); ++u )	{
						dateInfo = new DateInfo();
						dateInfo.year = _setDate.getFullYear();
						dateInfo.month = _setDate.getMonth() + 1;
						dateInfo.date = 0;
						dateInfo.day = u;
						dateInfo.toDay = false;
						dateInfo.xx = u;
						dateInfo.yy = 0;
						dateInfo.x = dateInfo.xx * _distance.x;
						dateInfo.y = dateInfo.yy * _distance.y;
						dataArr.push(dateInfo);
					}
				}
				
				dateInfo = new DateInfo();
				dateInfo.year = _setDate.getFullYear();
				dateInfo.month = _setDate.getMonth()+1;
				dateInfo.date = _setDate.getDate();
				dateInfo.day = _setDate.getDay();
				dateInfo.xx = dateInfo.day;
				dateInfo.yy = curLine;
				dateInfo.x = dateInfo.xx * _distance.x;
				dateInfo.y = dateInfo.yy * _distance.y;
				dateInfo.toDay = false;
				dataArr.push(dateInfo);
				totalDate = uint(dateInfo.date);
				
				//	오늘날짜에 해당하는 오브젝트를 찾아서 오늘날짜 정보를 넣어준다.
				if (dateInfo.year == _nowDate.getFullYear() && 
					dateInfo.month == _nowDate.getMonth()+1  &&
					dateInfo.date == _nowDate.getDate() )	{
					dateInfo.toDay = true;
				}
				
				if (dateInfo.day == 6)	curLine++;
			}
		}
		
		public function get year(): uint	{
			return _year;
		}
		public function set year( value: uint ): void	{
			_year = value;
			init( _year, _month, _nowDate, _distance );
		}
		public function get month(): uint	{
			return _month;
		}
		public function set month( value: uint ): void	{
			_month = value;
			if (_month < 1)	{
				_year--;
				_month = 12;
			}
			if (_month > 12)	{
				_year++;
				_month = 1;
			}
			init( _year, _month, _nowDate, _distance );
		}
	}
}

class DateInfo	{
	public var year:uint;
	public var month:uint;
	public var date:uint;
	public var day:uint;
	public var xx:uint;
	public var yy:uint;
	public var x:Number;
	public var y:Number;
	public var toDay:Boolean;
	function DateInfo(): void	{
		
	}
}